﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
using System.ComponentModel;

namespace TrayWeather.Utils
{
    /// <summary>
    /// Download text data from the specified URI
    /// </summary>
    class DownloadText
    {
        private BackgroundWorker textDownloader;

        public delegate void DownloadedEventHandler(string Result);
        public event DownloadedEventHandler DataDownloaded;

        public DownloadText()
        {
            textDownloader = new System.ComponentModel.BackgroundWorker();
        }

        /// <summary>
        /// Download text data from the specified URI
        /// </summary>
        /// <param name="Uri">Text data uri</param>
        /// <param name="Codepage">Text codepage</param>
        public void GetTextAsync(Uri Uri, Encoding Codepage)
        {
            textDownloader.DoWork += new DoWorkEventHandler(xmlDownloader_DoWork);
            textDownloader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(xmlDownloader_RunWorkerCompleted);

            GetTextArgs data = new GetTextArgs(Uri, Codepage);
            textDownloader.RunWorkerAsync(data);
        }

        /// <summary>
        /// Download text data from the specified URI
        /// </summary>
        /// <param name="Uri">Xml data uri</param>
        /// <param name="Codepage">Xml data codepage</param>
        /// <returns>Returns System.String with obteined data</returns>
        public string GetText(Uri Uri, Encoding Codepage)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri);
            if (Settings.Default.ConnectionType == 2)
            {
                request.Proxy = HttpWebRequest.GetSystemWebProxy();
            }
            else if (Settings.Default.ConnectionType == 3)
            {
                WebProxy proxy = new WebProxy(Settings.Default.ProxyServer);
                proxy.Credentials = new NetworkCredential(Settings.Default.ProxyLogin, Settings.Default.ProxyPass);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Codepage);

            return readStream.ReadToEnd();
        }



        private void xmlDownloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //Under Development
                //******************************************************

                //if (Settings.Default.DisplayNetworkErrors)
                //    throw e.Error;
            }
            else
            {
                this.DataDownloaded((string)e.Result);
            }
        }

        private void xmlDownloader_DoWork(object sender, DoWorkEventArgs e)
        {
            GetTextArgs data = ((GetTextArgs)e.Argument);

            Encoding encode = System.Text.Encoding.UTF8;
            if (data.dataCodepage != null)
                encode = data.dataCodepage;

            e.Result = GetText(data.dataUri, data.dataCodepage);
        }

        private struct GetTextArgs
        {
            public GetTextArgs(Uri XmlUri, Encoding Codepage)
            {
                dataUri = XmlUri;
                dataCodepage = Codepage;
            }
            public Uri dataUri;
            public Encoding dataCodepage;
        }
    }
}
